package com.gugee.data.config;


import com.gugee.common.web.config.NamedParameterJdbcTemplate;
import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * 路由访问查看
 * @author zzy
 * @Date 2019-07-18 15:25
 */
@Slf4j
@Aspect
@Component
public class InsDbAspect {

    @Autowired
    @Qualifier("newDataSource")
    DataSource dataSource;

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Pointcut(value = "execution(* com.gugee.ins..*.*Service.*(..))")
    public void Pointcut(){
    }

    @Around(value = "Pointcut()")
    public Object printInfo(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try{
            namedParameterJdbcTemplate.setDataSource(dataSource);
            JdbcTemplateHelper.setDataSource(dataSource);
            return proceedingJoinPoint.proceed();
        }catch (Exception e){
            throw e;
        }finally {
            namedParameterJdbcTemplate.removeDataSource();
            JdbcTemplateHelper.destroy();
        }
    }
}
